package com.zhkj.txg.module.mine.ui

import android.view.View
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
import com.zhkj.lib.base.ViewModelActivity
import com.zhkj.lib.http.HttpResponseStatus
import com.zhkj.txg.R
import com.zhkj.txg.module.mine.adapter.VisitLogAdapter
import com.zhkj.txg.module.mine.vm.VisitLogViewModel
import com.zhkj.txg.module.product.ui.ProductDetailActivity
import kotlinx.android.synthetic.main.activity_visit_log.*

class VisitLogActivity : ViewModelActivity<VisitLogViewModel>() {
    override fun setViewModel(): VisitLogViewModel = createViewModel()
    override fun statusTitleBar(): View? = titleVisitLog
    override val layoutResId: Int = R.layout.activity_visit_log

    private val visitAdapter = VisitLogAdapter().apply {
        loadMoreModule?.setOnLoadMoreListener {
            viewModel.getVisitLogList()
        }
        setOnItemClickListener { _, _, position ->
            if (!editMode) ProductDetailActivity.normalProduct(this@VisitLogActivity, data[position].goodsId.toInt())
            else {
                data[position].selected = !data[position].selected
                notifyItemChanged(position)
                refreshAllCheckStatus(false, checkAll = false)
            }
        }
    }

    override fun initUI() {
        with(rvVisitLog) {
            layoutManager = LinearLayoutManager(this@VisitLogActivity)
            adapter = visitAdapter
            (itemAnimator as DefaultItemAnimator).supportsChangeAnimations = false
        }
        visitAdapter.setEmptyView(R.layout.layout_empty_data)
        // 管理模式切换
        titleVisitLog.setRightClickListener(View.OnClickListener {
            titleVisitLog.setRightTextContent(getString(if (visitAdapter.editMode) R.string.edit else R.string.done))
            visitAdapter.editMode = !visitAdapter.editMode
            viewVisit.visibility = if (visitAdapter.editMode) View.VISIBLE else View.GONE
            visitAdapter.loadMoreModule?.isEnableLoadMore = !visitAdapter.editMode
            refreshVisit.setEnableRefresh(!visitAdapter.editMode)
        })
        // 全选按钮
        viewCheckAll.setOnClickListener {
            refreshAllCheckStatus(true, checkAll = !viewCheckAll.isSelected)
        }
        // 删除按钮
        tvVisitDelete.setOnClickListener {
            viewModel.deleteVisitLog(visitAdapter.getSelectedIds())
        }
        refreshVisit.setOnRefreshListener { initData() }
    }

    override fun initData() {
        viewModel.getVisitLogList(true)
    }

    override fun initViewModel() {
        viewModel.visitLogLiveData.observe(this, Observer { response ->
            when (response.status) {
                HttpResponseStatus.Status.START -> Unit
                HttpResponseStatus.Status.SUCCESS -> response.response?.let {
                    // 判断加载状态
                    if (!it.loadMore) refreshVisit.finishRefresh()
                    else if (it.hasMore) visitAdapter.loadMoreModule?.loadMoreComplete()
                    else visitAdapter.loadMoreModule?.loadMoreEnd(true)
                    // 数据填充
                    if (it.loadMore) visitAdapter.addData(it.data)
                    else visitAdapter.setNewData(it.data)
                }
                HttpResponseStatus.Status.FAILURE -> {
                    response.exception?.printStackTrace()
                    toast(response.statusTip)
                    if (viewModel.page > 1) visitAdapter.loadMoreModule?.loadMoreFail()
                    else refreshVisit.finishRefresh(false)
                }
            }
        })
        viewModel.deleteLogLiveData.observe(this, Observer {
            handlerResponseStatus(it.status, it.statusTip)
            if (it.status == HttpResponseStatus.Status.SUCCESS) {
                toast(it.response?.msg ?: getString(R.string.delete_success))
                viewModel.getVisitLogList(true)
            }
        })
    }

    private fun refreshAllCheckStatus(needCheckAll: Boolean, checkAll: Boolean) {
        visitAdapter.refreshSelectStatus(needCheckAll, checkAll)
        viewCheckAll.isSelected = visitAdapter.checkStatusEntity.allSelected
        tvVisitDelete.isEnabled = visitAdapter.checkStatusEntity.selectAny
    }
}